package multiThreading;

public class M {
    private int i=0;
    public void add(){
        i++;
        System.out.println(Thread.currentThread().getName()+"add为 "+i);
    }
    public synchronized void dec(){
        i--;
        System.out.println(Thread.currentThread().getName()+"dec为 "+i);
    }
    public static void main(String arg[]){
     M m=new M();
     add a=new add(m);
     dec d=new dec(m);
     for(int i=0;i<20;i++){
         new Thread(a).start();
         new Thread(d).start();
     }
    }
}
class add implements Runnable{
    private M m;
    public add(M m){
        this.m=m;
    }

    @Override
    public void run() {
        m.add();
    }
}
class dec implements Runnable{
    private M m;
    public dec(M m){
        this.m=m;
    }

    @Override
    public void run() {
        m.dec();
    }
}

